home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE02 / TPACK / TPACK.ZIP / WORKING.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-06-01  |  8.8 KB  |  282 lines

  1. {------------------------------------------------------------------------------}
  2. {UNREGISTERED VERSION (6/1/95) PLEASE REDISTRIBUTE IN tPACK.ZIP!
  3.  This revision does not contain everything, nor are the exciting
  4.  DataSetReporter and ExtendedMenu[Item] components included.
  5.  Use SWREG#5906 to receive these, icons and a help file for $130.
  6.  You must register when using this code in a business application!
  7.  You'll receive a license to use this code in up to 50 copies of
  8.  any app you write. In turn you will get responsive e-mail
  9.  tech support and enhancements till I run out of registrations
  10.  or suggestions. Meanwhile.. enjoy the code. Bye! I'll make more.
  11.  {(C)'1995 Michael/Ax-Systems, 71560,1754@Compuserve.com}
  12. {------------------------------------------------------------------------------}
  13.  
  14. unit Working;
  15.  
  16. interface
  17.  
  18. uses
  19.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  20.   Forms, Dialogs, StdCtrls, Buttons,
  21.   OkCore, PasUtils, MiscComp, ExtCtrls;
  22.  
  23. type
  24. {------------------------------------------------------------------------------}
  25. {TOkBoxForm defines the look the generic OkBox. Change this form or modify the
  26. look at runtime via TOk.OnActivate; Take care to keep the default events!
  27. attached to FormClose and ButtonClick; The Defaults are implemented at the end.}
  28.  
  29.   TWorkingMsgFormStop   = procedure(Sender: TObject;Var CanStop:Boolean) of object;
  30.  
  31.   TWorkingForm = class(TDemoForm)
  32.     StopLabel: TLabel;
  33.     StopButton: TBitBtn;
  34.     procedure StopButtonClick(Sender: TObject);
  35.     procedure FormCloseQuery(Sender: TObject; var CanStop: Boolean);
  36.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  37.   private
  38.     { Private declarations }
  39.   protected
  40.     { Protected declarations }
  41.     fOkClose: Boolean;
  42.     fOnOkStop: TWorkingMsgFormStop;
  43.   public
  44.     { Public declarations }
  45.     property OkClose:Boolean read fOkClose write fOkClose;
  46.     property OnOkStop: TWorkingMsgFormStop read fOnOkStop write fOnOkStop;
  47.   published
  48.     { Published declarations }
  49.   end;
  50.  
  51. {------------------------------------------------------------------------------}
  52.  
  53.   TOkFormActivate = procedure(StopForm:TForm;StopLabel:TLabel;StopButton:TButton) of object;
  54.  
  55.   TWorkingMsg = class(TOk)
  56.     Form:  TWorkingForm;
  57.   private
  58.     { Private declarations }
  59.     fVisible:        Boolean;
  60.     fOnFormActivate: TOkFormActivate;  {used to setup form.}
  61.   protected
  62.     { Protected declarations }
  63.     function BusyCount(Add:ShortInt):Integer;
  64.     procedure   SetActive(Flag:Boolean);             override;
  65.     procedure   SetVisible(Flag:Boolean);            Virtual;
  66.     procedure   DoOkStart(Var CanStart:Boolean);     override;
  67.     function    FreezeFormHandle:HWND;               override;
  68.     procedure   SetCritical(Flag:Boolean);           override;
  69.   public
  70.     { Public declarations }
  71.     constructor Create(AOwner:TComponent);           override;
  72.     procedure   Reset;                               virtual;
  73.     procedure   OkStopHandler(Sender:TObject;Var CanStop:Boolean);
  74.  
  75.     function IsNotBusy:Boolean;
  76.     procedure BusyReset;
  77.     procedure BusyOn;
  78.     procedure BusyMsg(const Text:String);
  79.     procedure BusyOff;
  80.  
  81.   published
  82.     { Published declarations }
  83.     property Visible:     Boolean      read fVisible    write SetVisible default true;
  84.     property OnFormActivate: TOkFormActivate read fOnFormActivate write fOnFormActivate;
  85.   end;
  86.  
  87. {------------------------------------------------------------------------------}
  88.  
  89. implementation
  90.  
  91. {$R *.DFM}
  92.  
  93. {------------------------------------------------------------------------------}
  94. {TOkBoxUForm.  *Here* is the OkBox Form code which defines ESSENTIAL events.
  95. You can completely redefine the Form via Ok.OnActivate; Below are the
  96. things that have to happen on the Form and CancelButton}
  97.  
  98. procedure TWorkingForm.StopButtonClick(Sender: TObject);
  99. begin
  100.   If Assigned(fOnOkStop) then   {if a OnOkStop proc is defined, allow closing.}
  101.     fOnOkStop(Sender,fOkClose);
  102. end;
  103.  
  104. procedure TWorkingForm.FormCloseQuery(Sender: TObject; var CanStop: Boolean);
  105. begin
  106.   StopButtonClick(Sender);
  107.   CanStop:=fOkClose;      {Form Property can be reset via fOnOkStop proc}
  108. end;
  109.  
  110. procedure TWorkingForm.FormClose(Sender: TObject; var Action: TCloseAction);
  111. begin
  112.  { Action:=caFree;}   {can't free all the time; keep one instance up to reset.}
  113. end;
  114.  
  115. {------------------------------------------------------------------------------}
  116.  
  117. constructor TWorkingMsg.Create(AOwner:TComponent);
  118. begin
  119.   inherited create(AOwner);
  120.   fVisible:=true;
  121. end;
  122.  
  123. procedure TWorkingMsg.SetVisible(Flag:Boolean);
  124. {must be much stronger on memory issues?
  125.   just not call activate if not visible!}
  126. begin
  127.   if flag<>fVisible then begin
  128.     fVisible:=Flag;
  129.     if assigned(Form) then
  130.       Form.Visible:=Flag;
  131.     end;
  132. end;
  133.  
  134. procedure TWorkingMsg.SetActive(Flag:Boolean);
  135. begin
  136.   if Enabled and Flag<>Active then begin
  137.     inherited SetActive(Flag);
  138.     if not Active and assigned(Form) then  {do we have one?}
  139.       Form.hide{Close};
  140.     end;
  141. end;
  142.  
  143. procedure TWorkingMsg.SetCritical(Flag:Boolean);
  144. {OkTry can not be stopped when in a critical section}
  145. var
  146.   l:boolean;
  147. begin
  148.   if flag<>Critical then begin
  149.     inherited SetCritical(Flag);
  150.     if assigned(Form) then
  151.       Form.StopButton.Enabled:=not Critical;
  152.     end;
  153. end;
  154.  
  155. procedure TWorkingMsg.DoOkStart(Var CanStart:Boolean);
  156. begin
  157.   Inherited DoOkStart(CanStart);
  158.   if CanStart then begin
  159.     Application.CreateForm(TWorkingForm,Form);
  160.     Form.OnOkStop:=OkStopHandler;
  161.     Form.OkClose:=True;
  162.     if assigned(fOnFormActivate) then
  163.       fOnFormActivate(tForm(Form),Form.StopLabel,Form.StopButton);
  164.     if fVisible then begin
  165.       Form.Show;
  166.       Form.Update;
  167.       end;
  168.     end;
  169. end;
  170.  
  171. function TWorkingMsg.FreezeFormHandle:HWND;
  172. begin
  173.   result:=Form.Handle;
  174. end;
  175.  
  176. procedure TWorkingMsg.OkStopHandler(Sender:TObject;Var CanStop:Boolean);
  177. begin
  178.   if Critical then                {can not exit in a critical section!}
  179.     CanStop:=False               {let the user beware!}
  180.   else begin
  181.     Active:=False;  {try to turn off.. ancestor will now call user's CanCanel proc}
  182.     CanStop:=Stop; {if that proc concurs, we allow the OkBoxform to close}
  183.      {That's all. The Active Flag has already been set to false when we return}
  184.     end;
  185. end;
  186.  
  187. procedure TWorkingMsg.Reset;
  188. var                            {not done}
  189.   OrgOnOkStop: TOkOnOkStop;
  190.   OrgOkClose:  Boolean;
  191. {unconditional deactivate. very useful while setting up OkTryes. put a call
  192. on a button somewhere to allow you to break out of things regardless of what you
  193. coded! very helpful during development (to me anyway) it is not used inside here}
  194. begin
  195.   inherited Reset;
  196.   if assigned(Form) then  {do we have one?}
  197.     with Form do begin     {begin manual override <g>}
  198.       OnOkStop:=nil;            {no denying the exit this time}
  199.       OkClose:=True;          {manually allow Close-- that'll set the vars.}
  200.       end;
  201.   Active:=False;
  202.   if assigned(Form) then begin
  203.     Form.Free;
  204.     Form:=nil;
  205.     end;
  206. end;
  207.  
  208.  
  209.  
  210. {------------------------------------------------------------------------------}
  211. { BUSY..                                                                       }
  212. {------------------------------------------------------------------------------}
  213. {the following is a set of procs to manage telling the user that we're busy.}
  214. {in order to implement this well, we're tracking how often the box has been turned
  215. on and off, placing it and removing it as needed.}
  216.  
  217.  
  218. {this is a little out of context and would/could cause trouble when exceptions occour}
  219.  
  220.  
  221. function TWorkingMsg.BusyCount(Add:ShortInt):Integer;
  222. {keep track of how often we've been turned on.}
  223. {parameters used to combine get/set}
  224. const
  225.   Count:Integer=0;
  226.   c:TCursor=crDefault;
  227. begin
  228.   Count:=Count+Add;
  229.   if Count<=0 then begin
  230.     Count:=0;
  231.     Screen.Cursor:=c;
  232.     end
  233.   else
  234.     if Count=Add then begin {just turned on}
  235.       c:=Screen.Cursor;
  236.       Screen.Cursor:=crHourGlass;
  237.       end;
  238.   Result:=Count;
  239. end;
  240.  
  241. function TWorkingMsg.IsNotBusy:Boolean;
  242. {inquire about the box status and syncronize the counter if it's off}
  243. begin
  244.   Result:=Stop;
  245.   if Result then
  246.     if BusyCount(0)>0 then
  247.       BusyCount(-BusyCount(0));
  248. end;
  249.  
  250. procedure TWorkingMsg.BusyOn;
  251. {Turn it on anyway!! (resetting the box) and up the counter}
  252. begin
  253.   OkOn;
  254.   BusyCount(1);
  255. end;
  256.  
  257. procedure TWorkingMsg.BusyMsg(const Text:String);
  258. {could stall! this is why this functionality belongs into StopBox! and TOK!}
  259. begin
  260.   Form.StopLabel.Caption:=Text;
  261. end;
  262.  
  263. procedure TWorkingMsg.BusyOff;
  264. {turn off only when we're counting back to 0 and it's on in the second place}
  265. begin
  266.   if BusyCount(0)=1 then
  267.     if not IsNotBusy then
  268.       OkOff;
  269.   BusyCount(-1);
  270. end;
  271.  
  272. procedure TWorkingMsg.BusyReset;
  273. begin
  274.   if BusyCount(0)>0 then
  275.     BusyCount(-BusyCount(0));
  276.   BusyOff;
  277. end;
  278.  
  279.  
  280.  
  281. end.
  282.